<?php
/*
 * Created on 2013-1-26
 *
 */
include_once dirname(__FILE__)."/basedao.php";
include_once dirname(__FILE__)."/../objects/user.php";
class UsersDAO extends BaseDAO{
	/**
	 * 添加用户
	 */
	public function insertUser($user){
		$this->getConn()->query("LOCK TABLES users WRITE");
		$sqlstr = "INSERT INTO users(user_name, user_psw, user_nickname, user_email, user_img, user_birth) VALUES(?,?,?,?,?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("ssssss",
				$user->getUserName(),
				$user->getUserPsw(),
				$user->getUserNickName(),
				$user->getUserEmail() ,
				$user->getUserImg(),
				$user->getUserBirth());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 修改用户资料
	 */
	public function updateUser($user){
		$this->getConn()->query("LOCK TABLES users WRITE");
		$sqlstr = "UPDATE users SET user_psw=?, user_nickname=?, user_email=?, user_active =?, user_img=?, user_birth=? WHERE user_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("sssssi",
				$user->getUserPsw(),
				$user->getUserNickName(),
				$user->getUserEmail() ,
				$user->getUserActive(),
				$user->getUserImg(),
				$user->getUserBirth(),
				$user->getUserId());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 根据ID查找一个用户
	 *
	 */
	 public function getUserInfoById($user_id){
		$this->getConn()->query("LOCK TABLES users READ");
		$sqlstr = "SELECT user_id, user_name, user_psw, user_nickname, user_email, user_img, user_birth FROM users WHERE user_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $user_id);
		$stmt->execute();
		$user = new User();
		$stmt->bind_result($user_id, $user_name, $user_psw, $user_nickname, $user_email, $user_img, $user_birth);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$user->setUserId($user_id);
		$user->setUserName($user_name);
		$user->setUserPsw($user_psw);
		$user->setUserNickName($user_nickname);
		$user->setUserEmail($user_email);
		$user->setUserImg($user_img);
		$user->setUserBirth($user_birth);
		return $user;
	 }
	 /**
	  * 根据用户名查找一个用户
	  */
	 public function getUserInfoByName($user_name){
		$this->getConn()->query("LOCK TABLES users READ");
		$sqlstr = "SELECT user_id, user_name, user_psw, user_nickname, user_email, user_img, user_birth FROM users WHERE user_name=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("s", $user_name);
		$stmt->execute();
		$user = new User();
		$stmt->bind_result($user_id, $user_name, $user_psw, $user_nickname, $user_email, $user_img, $user_birth);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$user->setUserId($user_id);
		$user->setUserName($user_name);
		$user->setUserPsw($user_psw);
		$user->setUserNickName($user_nickname);
		$user->setUserEmail($user_email);
		$user->setUserImg($user_img);
		$user->setUserBirth($user_birth);
		return $user;
	 }
	 /**
	  * 取得所有用户
	  */
	 public function getAllUsers(){
	 	$users = array();
		$this->getConn()->query("LOCK TABLES users READ");
		$sqlstr = "SELECT user_id, user_name, user_psw, user_nickname, user_email, user_img, user_birth FROM users";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($user_id, $user_name, $user_psw, $user_nickname, $user_email, $user_img, $user_birth);
		while($stmt->fetch()){
			$user = new User();
			$user->setUserId($user_id);
			$user->setUserName($user_name);
			$user->setUserPsw($user_psw);
			$user->setUserNickName($user_nickname);
			$user->setUserEmail($user_email);
			$user->setUserImg($user_img);
			$user->setUserBirth($user_birth);
			array_push($users,$user);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $users;
	 }
}
//date_default_timezone_set('Asia/Shanghai');
//$u = new User();
//$u->setUserName("utest");
//$u->setUserPsw("psw");
//$u->setUserNickName("utest");
//$u->setUserEmail("t@t.com");
//$u->setUserImg("pic.png");
//$u->setUserBirth(date("Y-m-d H:i:s"));
//$ud = new UsersDAO();
//$ud->insertUser($u);
//$user = $ud->getUserInfoByName($u->getUserName());
//echo $user->getUserName();

//////////////////////////////
//
//$ud = new UsersDAO();
//$user = $ud->getUserInfoByName("abc");
//////////////////////////////
//
//
//$ud = new UsersDAO();
//$users = $ud->getAllUsers();
//print_r($users);
?>
